package flatlandRPG;

import org.lwjgl.util.vector.Vector2f;

// -------------------------------------------------------------------------
/**
 * A trigger class. Activates the trigger when the player uses the 'use' key in
 * the trigger area.
 *
 * @author evant
 * @version Apr 29, 2011
 */
public class Trigger
    extends Entity
    implements ILogic
{
    private Action action;
    private Player player;
    private float  radius;
    private boolean  activated;


    // ----------------------------------------------------------
    /**
     * Create a new Trigger object.
     *
     * @param player
     *            the player
     * @param location
     *            the location of the trigger
     * @param radius
     *            the radius of the trigger
     * @param action
     *            the action that happens
     */
    public Trigger(Player player, Vector2f location, float radius, Action action)
    {
        activated = false;
        this.action = action;
        this.player = player;
        this.radius = radius;
        setLocation(location);
    }


    public void doLogic()
    {
        if (VectorUtil.distance(player.getLocation(), getLocation()) <= radius)
        {
            action.activate();
            activated = true;
        }
    }


    // ----------------------------------------------------------
    /**
     * Returns if the trigger has been activated before. This is useful for
     * triggers you only want to run once.
     *
     * @return true if the trigger has been activated, false otherwise.
     */
    public boolean hasActivated()
    {
        return activated;
    }

}
